package org.september.pisces.user.permission.controller;

import java.util.List;

import org.september.core.constant.enums.DeleteFlag;
import org.september.core.exception.BusinessException;
import org.september.pisces.user.permission.entity.Department;
import org.september.pisces.user.permission.service.DeptService;
import org.september.pisces.user.permission.service.OperationLogService;
import org.september.simpleweb.controller.BaseController;
import org.september.simpleweb.model.ResponseVo;
import org.september.smartdao.CommonValidator;
import org.september.smartdao.common.BaseService;
import org.september.smartdao.model.Page;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;

@Controller
public class DepartmentController extends BaseController{

	@Autowired
	private BaseService baseService;
    @Autowired
    private CommonValidator commonValidator;
    
    @Autowired
    private DeptService deptService;
    
    @Autowired
    private OperationLogService operationLogService;
    
    @RequestMapping(value = List_Page)
    public ModelAndView DepartmentList() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }
    
    @ResponseBody
    @RequestMapping(value = List_Data)
    public ResponseVo<Page<Department>> listDepartmentData(Page<Department> page , Department vo,String sortField , String sortOrder) throws Exception {
        ParamMap pm = new ParamMap(vo);
        page = baseService.getCommonDao().findPageByParams(Department.class , page, "Department.listDepartment", pm);
        return ResponseVo.<Page<Department>> BUILDER().setData(page).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }
    
    @RequestMapping(value = "/select")
    public ModelAndView select() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }
    
    @ResponseBody
    @PostMapping(Get_Data)
    public ResponseVo<List<Department>> getData() {
    	Department vo = new Department();
        vo.setDeleteFlag(DeleteFlag.NotDelete.ordinal());
        List<Department> list = baseService.getCommonDao().listByExample(vo);
        return ResponseVo.<List<Department>>BUILDER().setData(list).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Add_Page)
    public ModelAndView addDepartment(Long parentId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Department parent = baseService.getCommonDao().get(Department.class, parentId);
        mv.addObject("parent", parent.getName());
        mv.addObject("parentId", parentId);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Add_Action)
    public ResponseVo<String> doAddDepartment(@Valid Department department) throws Exception {
    	Department parent = baseService.getCommonDao().get(Department.class, department.getParentId());
    	if(parent==null) {
    		throw new BusinessException("上级部门不存在！");
    	}
    	department.setParentId(parent.getId());
    	if(commonValidator.exsits(Department.class, new String[] {"name","deleteFlag"}, new Object[] {department.getName() , DeleteFlag.NotDelete.ordinal()})) {
			throw new BusinessException("该部门已存在，创建失败！");
		}
    	if(commonValidator.exsits(Department.class, new String[] {"code","deleteFlag"}, new Object[] {department.getCode() , DeleteFlag.NotDelete.ordinal()})) {
			throw new BusinessException("部门编码已存在，创建失败！");
		}
    	department.setFullParentId(parent.getFullId());
        baseService.save(department);
        operationLogService.addLog("添加了部门【"+department.getName()+"】");
        return ResponseVo.<String> BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }
    
    @RequestMapping(value = Edit_Page)
    public ModelAndView editDepartment(Long id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Department po = baseService.getCommonDao().get(Department.class, id);
        Department parent = baseService.getCommonDao().get(Department.class, po.getParentId());
        if(parent!=null) {
        	po.setParent(parent.getName());
        }
        mv.addObject("department", po);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Edit_Action)
    public ResponseVo<String> doUpdateDepartment(@Valid Department vo) throws Exception {
    	Department po = baseService.getCommonDao().get(Department.class, vo.getId());
    	
    	if(commonValidator.exsitsNotMe(Department.class, new String[] {"name","deleteFlag"}, new Object[] {vo.getName() , DeleteFlag.NotDelete.ordinal()}, vo.getId())) {
			throw new BusinessException("该部门已存在，请修改后再重试！");
		}
        
    	po.setName(vo.getName());
    	po.setDescb(vo.getDescb());
    	po.setCode(vo.getCode());
//        po.setParentId(vo.getParentId());
//        po.setFullParentId(vo.getFullParentId());
        baseService.updateWithNullFields(po);
        operationLogService.addLog("修改了部门【"+po.getName()+"】");
        return ResponseVo.<String> BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }
    
    @ResponseBody
    @RequestMapping(value = Delete_Action)
    public ResponseVo<String> delete(Long id) throws Exception {
        Department po = baseService.getCommonDao().get(Department.class, id);
        if (po == null) {
            throw new BusinessException("数据不存在或已删除");
        }
        //部门下面的用户是否为空
        if(deptService.countAllUser(po.getFullId())>0) {
        	throw new BusinessException("部门下用户不为空，不能删除");
        }
        baseService.delete(po);
        operationLogService.addLog("删除了部门【"+po.getName()+"】");
        return ResponseVo.<String> BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    private static final String Mapping_Prefix = "/department";
	public static final String List_Page = Mapping_Prefix+"/departmentList";
	public static final String List_Data = Mapping_Prefix+"/listDepartmentData";
	public static final String Get_Data = Mapping_Prefix+"/getData";
	
	public static final String Add_Page = Mapping_Prefix+"/addDepartment";
	public static final String Add_Action = Mapping_Prefix+"/doAddDepartment";
	
	public static final String Edit_Page = Mapping_Prefix+"/editDepartment";
	public static final String Edit_Action = Mapping_Prefix+"/doUpdateDepartment";
	public static final String Delete_Action = Mapping_Prefix+"/delete";
}
